package it.polimi.TravelDream.ejb.entities;

import it.polimi.TravelDream.ejb.dtos.FlightDTO;

import java.io.Serializable;
import java.lang.String;
import java.util.Date;
import java.util.List;

import javax.persistence.*;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * Entity implementation class for Entity: Flight
 *
 */
@Entity
@Table(name = "flights")
//Devo prendere solo quelli disponibili? O mi interessano vedere tutti i possibili voli?
@NamedQueries({
    @NamedQuery(name = Flight.FIND_FLIGHTS_BY_DEPARTURE_DATE, query = "SELECT f FROM Flight f WHERE f.departureDate = :date"),
    @NamedQuery(name = Flight.FIND_FLIGHTS_BY_DEPARTURE_CITY, query = "SELECT f FROM Flight f WHERE f.departureCity = :city "),
    @NamedQuery(name = Flight.FIND_FLIGHTS_BY_ARRIVAL_CITY, query = "SELECT f FROM Flight f WHERE f.arrivalCity = :city "),
    @NamedQuery(name = Flight.FIND_FLIGHTS_BY_DEPARTURE_CITY_AND_DATE, query = "SELECT f FROM Flight f WHERE f.departureCity = :city and f.departureDate= :date"),
    @NamedQuery(name = Flight.FIND_FLIGHTS_BY_DEPARTURE_AIRPORT, query = "SELECT f FROM Flight f WHERE f.departureAirport = :airport "),
    @NamedQuery(name = Flight.FIND_FLIGHTS_BY_ARRIVAL_AIRPORT, query = "SELECT f FROM Flight f WHERE f.arrivalAirport = :airport "),
    @NamedQuery(name = Flight.FIND_ALL_FLIGHT, query = "SELECT f FROM Flight f"),
    @NamedQuery(name = Flight.SEARCH_FLIGHT, query = "SELECT f FROM Flight f WHERE f.departureDate = :departureDate and f.departureCity = :departureCity and f.departureAirport = :departureAirport and f.arrivalAirport = :arrivalAirport and "
    		+ "f.arrivalCity= :arrivalCity and f.available = 'TRUE'"),
    @NamedQuery(name = Flight.FIND_FLIGHTS_BY_DATE_DEPARTURE_AND_ARRIVAL, query = "SELECT f FROM Flight f WHERE f.departureDate = :departureDate and f.departureCity = :departureCity and f.arrivalCity= :arrivalCity and f.available = true"),
    @NamedQuery(name = Flight.SEARCH_FLIGHTS_BY_PERIOD_AND_ARRIVAL_CITY, query = "Select f From Flight f Where (f.departureDate>=:startingDate Or f.departureDate<=:expiringDate) and"
    		+ "(f.arrivalCity=:arrivalCity or f.departureCity=:departureCity)")

})
public class Flight implements Serializable {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int flightID;
//	@Temporal(TemporalType.TIME)
//	private Date departureTime;
	@Column(length = 45)
	@NotEmpty
	private String arrivalAirport;
	@Column(length = 45)
	@NotEmpty
	private String departureCity;
	@Temporal(TemporalType.DATE)
	private Date departureDate;
	@Column(length = 45)
	@NotEmpty
	private String arrivalCity;
	private double price;
	private String seat;
	private boolean available = true;
	@Column(length = 45)
	@NotEmpty
	private String departureAirport;

	//Volo contenuto nel pacchetto scelto dall'utente
	@ManyToOne(optional = false)
	@JoinColumn(name = "userpackages_travelID")
	private UserPackage userPackage;

	//Pacchetti in cui sono contenute le camere
	@ManyToMany(mappedBy = "flights")
	private List <BasicPackage> list_of_package;

	private static final long serialVersionUID = 1L;
	
	public static final String FIND_FLIGHTS_BY_DEPARTURE_DATE = "Flight.findFlightsByDepartureDay";
	public static final String FIND_FLIGHTS_BY_DEPARTURE_CITY = "Flight.findFlightsByDepartureCity";
	public static final String FIND_FLIGHTS_BY_ARRIVAL_CITY = "Flight.findFlightsByArrivalCity";
	public static final String FIND_FLIGHTS_BY_DEPARTURE_CITY_AND_DATE = "Flight.findFlightsByDepartureCityAndDate";
	public static final String FIND_FLIGHTS_BY_DEPARTURE_AIRPORT = "Flight.findFlightsByDepartureAirport";
	public static final String FIND_FLIGHTS_BY_ARRIVAL_AIRPORT = "Flight.findFlightsByArrivalAirport";
	public static final String FIND_ALL_FLIGHT = "Flight.findAllFlight";
	public static final String SEARCH_FLIGHT = "Flight.searchFlight";
	public static final String FIND_FLIGHTS_BY_DATE_DEPARTURE_AND_ARRIVAL ="FLight.findFlightsByDateDepartureAndArrival";
	public static final String SEARCH_FLIGHTS_BY_PERIOD_AND_ARRIVAL_CITY = "Flight.searchFlightsByPeriodAndArrivalCity";


	public Flight() {
		super();
	}   
	
	public Flight(FlightDTO flightDTO) {
		super();
//		this.departureTime = flightDTO.getDepartureTime();
		this.arrivalAirport = flightDTO.getArrivalAirport();
		this.departureCity = flightDTO.getDepartureCity();
		this.departureDate = flightDTO.getDepartureDate();
		this.arrivalCity = flightDTO.getArrivalCity();
		this.price = flightDTO.getPrice();
		this.seat = flightDTO.getSeat();
		this.available = flightDTO.isAvailable();
		this.departureAirport = flightDTO.getDepartureAirport();
	} 
	
	public int getFlightID() {
		return this.flightID;
	}

	public void setFlightID(int flightID) {
		this.flightID = flightID;
	} 
	/*
	public Date getDepartureTime() {
		return this.departureTime;
	}

	public void setDepartureTime(Date departureTime) {
		this.departureTime = departureTime;
	}  
	*/ 
	public String string() {
		return this.arrivalAirport;
	}

	public void setArrivalAirport(String arrivalAirport) {
		this.arrivalAirport = arrivalAirport;
	}   
	public String getDepartureCity() {
		return this.departureCity;
	}

	public void setDepartureCity(String departureCity) {
		this.departureCity = departureCity;
	}   
	public Date getDepartureDate() {
		return this.departureDate;
	}

	public void setDepartureDate(Date departureDate) {
		this.departureDate = departureDate;
	}   
	public String getArrivalCity() {
		return this.arrivalCity;
	}

	public void setArrivalCity(String arrivalCity) {
		this.arrivalCity = arrivalCity;
	}   
	public double getPrice() {
		return this.price;
	}

	public void setPrice(double price) {
		this.price = price;
	}   
	public String getArrivalAirport() {
		return arrivalAirport;
	}

	public String getSeat() {
		return this.seat;
	}

	public void setSeat(String seat) {
		this.seat = seat;
	}   
	public boolean getAvailable() {
		return this.available;
	}

	public void setAvailable(boolean available) {
		this.available = available;
	}   
	public String getDepartureAirport() {
		return this.departureAirport;
	}

	public void setDepartureAirport(String departureAirport) {
		this.departureAirport = departureAirport;
	}

	public UserPackage getUserPackage() {
		return userPackage;
	}

	public void setUserPackage(UserPackage userPackage) {
		this.userPackage = userPackage;
	}

	public List<BasicPackage> getList_of_package() {
		return list_of_package;
	}

	public void setList_of_package(List<BasicPackage> list_of_package) {
		this.list_of_package = list_of_package;
	}
   
}
